package com.lpc.kotlintest

import org.junit.Assert.assertEquals
import org.junit.Test
import java.nio.file.Files
import java.nio.file.Paths

/**
 * 基础
 *
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {

    @Test
    fun addition_isCorrect() {

        for (i in 1..5 step 2) {
            println(i)
        }

        assertEquals(4, 2 + 2)
    }

    @Test
    fun addition_isCorrect2() {

        val fruits = listOf("banana", "avocado", "apple", "kiwi")

        fruits.filter { !it.equals("banana") }
                .map {
                    it.toUpperCase()
                }.forEach { println(it) }

        assertEquals(4, 2 + 2)
    }

    @Test
    fun addition_isCorrect3() {

        testLazy()
    }

    @Test
    fun addition_isCorrect4() {

        var s = "123"
        s.test(3)
    }

    @Test
    fun addition_isCorrect5() {

        println(RR.name)
    }

    @Test
    fun addition_isCorrect6() {

        val stream = Files.newInputStream(Paths.get("/some/file.txt"))
        stream.buffered().reader().use { reader ->
            println(reader.readText())
        }
    }

    @Test
    fun addition_isCorrect7() {

        var i = com.lpc.kotlintest.Test()

        println("i ${i.add(3, 4)}")
    }

    @Test
    fun addition_isCorrect8() {

//        var ints = intArrayOf(1, 2, 3)
//        ints[0] = ints[1] + ints[2]
//        println("${ints[0]}")

        var ints = Array(3, { i -> i * i })
        println("${ints[2].toString()}")
    }

    @Test
    fun addition_isCorrect9() {

        val text = """
            for (c in "foo")
            print(c)
        """

        println("${text.toString()}")
    }

    @Test
    fun addition_isCorrect10() {

        var i = 3
        var b = { i > 3 ?: 3 }
    }

    fun testLazy() {

        val str: String by lazy {

            "123"
        }

        lateinit var s: String
        s = "123"

        s = "456"

        println("str $str")
        println("s $s")
    }

    fun String.test(i: Int) {

        println("str ${i + 1}")
    }

    object RR {

        var name = "123"
            get() = field + "123"
            set(value) {
                field = value + "123"
            }
    }
}
